/**   
*    
* 项目名称：HMS   
* 类名称：DateUtil   
* 类描述：   
* 创建人：陈晶学   
* 创建时间：Mar 7, 2012 11:02:02 AM       
* 修改备注：   
* @version  1.0  
*    
*/ 
package com.ad.common.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;


public class DateUtil {

	/**
	 * 两个日期相差天数(yyyy-MM-dd or yyyy-MM-dd)
	 */
	public static int subGetDay(String dateStr1,String dateStr2) throws Exception{
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		
		Date date1 = sdf.parse(dateStr1);
		
		Date date2 = sdf.parse(dateStr2);
		
		int subDays = (int)((date1.getTime() - date2.getTime())/(1000*60*60*24));
		
		return subDays;
		
	}
	
	/**
	 * 两个日期相差天数(yyyy-MM-dd or yyyy-MM-dd)
	 */
	public static int subGetDay(Date date1,Date date2) throws Exception{
		
		int subDays = (int)((date1.getTime() - date2.getTime())/(1000*60*60*24));
		
		return subDays;
		
	}
	

	
	/**
	 * 指定一个时间，获取该时间的前后N天的时间
	 * @param theOtherDays 如前N天为：-N； 后N天为：N
	 * @param theTime 从这天开始计算
	 * @param format 返回的时间格式
	 * @return
	 */
	public static String getTheOtherDay(int theOtherDays, Date theTime,
			String format) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(new Date());
		cal.add(Calendar.DATE, -3);
		return new SimpleDateFormat(format).format(cal.getTime());
	}
	
	public static String getTheOtherDay(int theOtherDays, String format){
		return getTheOtherDay(theOtherDays, new Date(), format);
	}
	
	public static void main(String[] args)throws Exception {
		
		System.out.println(subGetDay("2012-12-02","2012-11-23"));
	}
	
	
}
